<template>
  <div :style="{width:'100%',height:'100%'}" ref="homeEch"></div>
</template>

<script>
export default {
  name: "Age",
  data() {
    return {
      data: [
      ],
      agelist: [{}]
    };
  },
  created() {
    // this.echarts();
  },
  mounted() {  
    this.echarts();
    this.resizefun = () => {
      this.$echarts.init(this.$refs.homeEch).resize();
    };
    window.addEventListener("resize", this.resizefun);
  },
  //移除事件监听，避免内存泄漏
  beforeDestroy() {
    window.removeEventListener("resize", this.resizefun);
    this.resizefun = null;
  },
  methods: {
    // 年龄比例接口
    echarts() {
        let that = this;
      let myChart = this.$echarts.init(this.$refs.homeEch);
    
        let data = {
            names: ['初中及以下', '高中', '大专', '本科'],
            values: [{
                name: '初中及以下', value: 1196
            },
            {
                name: '高中', value: 346
            },
            {
                name: '大专', value: 307
            },
            {
                name: '本科', value: 186
            },]
        }
        let valuelist = data.values.map(
            function (item, index) {
                return {
                    name: item.name,
                    value: item.value,
                    itemStyle: {
                        normal: {
                            color: that.$colors[index]
                        }
                    }
                }
            }
        )
        var option = {
            tooltip: {
                trigger: 'item',
                formatter: "{a} <br/>{b} : {c} ({d}%)"
            },

            legend: {
                left: 0,
                orient: 'horizontal',
                top: 0,
                data: data.names,
                textStyle: {
                    color: "#fff"
                }
            },
            series: [
                {
                    itemStyle: {
                        normal: {
                            label: {
                                show: false
                            },
                            labelLine: {
                                show: false
                            }
                        }
                    },
                    name: '人数',
                    type: 'pie',
                    radius: '55%',
                    center: ['50%', '60%'],
                    data: valuelist

                }
            ]
        };
        myChart.setOption(option);
    }
  }
};
</script>

<style>
</style>